<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\BaoxiaoCycleModel;
use app\admin\model\BaoxiaoModel;
use app\services\JsonService;
use app\services\UtilService as Util;
use app\validate\BaoxiaoValidate;
use cmf\controller\AdminBaseController;
use cmf\lib\Upload;
use think\Db;
use think\exception\ValidateException;

class BaoxiaoController extends AdminBaseController
{
    function index(){
        $list = BaoxiaoCycleModel::getListAll();
        if(!$list)$list = [];
        $this->assign('cycle_list',$list);
        $count = BaoxiaoCycleModel::getIngCount();
        $this->assign('count',$count);
        return $this->fetch();
    }

    //获取报销数据
    public function data()
    {
        $current_cycle_id = BaoxiaoCycleModel::order('id desc')->value('id',0);
        $page       = $this->request->param('page','1');
        $limit      = $this->request->param('limit','10');
        $cycle_id   = $this->request->param('cycle_id',$current_cycle_id);
        $model      = new BaoxiaoModel();

        $res        = $model->getListById($page,$limit,$this->role_id,$cycle_id);
        $count      = $model->getCount($this->role_id,$cycle_id);
        if($res){
            JsonService::successlayui($count,$res,'数据列表');
        }else{
            JsonService::fail(BaoxiaoModel::getErrorInfo());
        }
    }

    //报销信息
    public function info()
    {
        $id         = $this->request->param('baoxiao_id');
        $use_list   = BaoxiaoModel::BAOXIAO_USE;
        $info       = [];
        $info['use']       = 0;
        if($id){
            $info = BaoxiaoModel::getInfo($id);
            if(!$info){
                abort(500,BaoxiaoModel::getErrorInfo());
            }
        }
        $this->assign('use_list',$use_list);
        $this->assign('info',$info);
        return $this->fetch('form');
    }

    public function operate()
    {
        try {
            validate(BaoxiaoValidate::class)->check($this->request->param());
        } catch (ValidateException $e) {
            JsonService::fail($e->getError());
        }
        $data = Util::postMore([
            ['baoxiao_id', 0],
            'money',
            'use',
            'department',
            'img',
            'remark',
        ]);
        $data['img'] = implode(',',$data['img']);
        $data['apply_time'] = date('Y-m-d H:i:s');
        $employee_id = session('EMPLOYEE_ID');
        if(!$employee_id){
            JsonService::fail('无权限！');
        }
        $data['employee_id'] = $employee_id;
        $baoxiaoModel = new BaoxiaoModel();
        if($data['baoxiao_id']){
            $re = $baoxiaoModel->edit($data['baoxiao_id'],$data);
        }else{
            $CycleModel = new BaoxiaoCycleModel();
            $cycle_id = $CycleModel->where('status',1)->value('id');
            if(!$cycle_id){
                JsonService::fail('不在报销时段！');
            }
            $data['cycle_id'] = $cycle_id;
            $re = $baoxiaoModel->create($data);
        }
        if($re){
            JsonService::success('操作成功');
        }else{
            JsonService::fail('操作失败');
        }
    }

    //审核页面
    public function review()
    {
        return $this->fetch();
    }

    //审核数据
    public function review_data()
    {
        $model      = new BaoxiaoModel();
        $res        = $model->getReviewList($this->role_id);
        $count      = $model->getReviewCount($this->role_id);
        if($res){
            JsonService::successlayui($count,$res,'数据列表');
        }else{
            JsonService::fail(BaoxiaoModel::getErrorInfo());
        }
    }

    public function do_review()
    {
        $id     = $this->request->param('id');
        $type   = $this->request->param('type');
        $model      = new BaoxiaoModel();
        $res        = $model->doReview($id,$type,$this->role_id);
        if($res){
            JsonService::success('操作成功');
        }else{
            JsonService::fail(BaoxiaoModel::getErrorInfo());
        }
    }


    public function upload()
    {
        $uploader   = new Upload();
        $result     = $uploader->upload();
        if ($result === false) {
            JsonService::fail($uploader->getError());
        } else {
            JsonService::success("上传成功!",$result);
        }
    }

    public function get_img()
    {
        $id = $this->request->param('id');
        $img = BaoxiaoModel::where('id',$id)->value('img');
        if(!$img){
            JsonService::fail('没有上传图片!');
        }
        $img_array = explode(',',$img);
        $data = [];
        foreach ($img_array as $k=>$v){
            $image['title'] = '相册';
            $image['src'] = $v;
            $image['thumb'] = $v;
            $data[] = $image;
        }
        $return['title']    = '相册';
        $return['id']       = '123';
        $return['data']     = $data;
        return json($return);
    }

}
